"use strict";
// 组合构造函数与原型
   
function Gmccui(){
	var _self = this;
	_self.domainUrl = "http://localhost:9002/information/api/v1/";
	_self.apiConfig = {'id':'','url':'','type':'','async':true,'contentType':'application/json;charset=utf-8'};
	_self.api = {};
	_self.headerMap = {};
	return _self;
	
}

//通用方法封装
Gmccui.prototype = {
	constructor:Gmccui,
	transGetParams:function(_params){
		//OBEJCT格式处理成get方式所需要的数据格式，统一传递的参数都为JSON对象
		var _resultParams = "";
		var _count = 0;
		for(var _key in _params){
			_resultParams += (_count===0?'':'&')+_key+'='+_params[_key];
			++_count;
		}
		return _resultParams;
	},
	setHeaderMap:function(_map){
		var _isObject = $.type(_map) === "object";
		if(_isObject){
			this.headerMap = $.extend(this.headerMap,_map);
		}else{
			throw Error("参数必须为OBJECT");
		}
		//return this;
	},
	setReqHeader:function(_request){
			//写入请求头
			for(var key in this.headerMap){
				_request.setRequestHeader(key,this.headerMap[key]);
			}
			return _request;

	},
	setOneApi:function(_id,_url){
		//提供给外部写入数据接口，并通过别名使用
		this.api[_id] = {"url":this.domainUrl+_url};
		return this.api;
	},
	setMutiApi:function(_arr){
		if(_arr instanceof Array){
			for(var i=0,len=_arr.length;i<len;i++){
			this.setOneApi(_arr[i]['id'],_arr[i]['url']);
		}
		} else{
			throw Error('参数必须为数组！');
			return !1;
		}
		
	},
	getApi:function(_id){
		return this.api[_id];
	},
	handle:function(_config,_data,_then){
		//简单封装jquery的AJAX，方便使用
		//todo
		if($.type(_data) === "function"){
			_then = _data;
			_data = null;
		}
		//预判断
		var _URLMAP = this.getApi(_config['id']);
		if(typeof _URLMAP === 'undefined'){
			throw Error(_config['id']+'___无对应该别名的URL');
		}

		$.ajax({
		      'url': _URLMAP['url'],//请求的url
		      'data': _config['type'] === 'post'?JSON.stringify(_data):this.transGetParams(_data),//请求的参数
		      'type': _config['type'],//请求的类型
		      'dataType': 'json',//接收数据类型
		      'contentType':this.apiConfig['contentType'],
		      'async': _config['async'],//异步请求
		      'cache': false,//浏览器缓存
		      'success': function (_callback) {
		      	//请求成功后执行的函数
		      	//if(_callback.hasOwnProperty('returnFlag')&&_callback['returnFlag']===1){
					_then(_callback);
		      	//}else{
		      		//throw Error(_callback['errorMsg']);
		      	//}
		      	$.each(_callback,function(Index,Obj){
		      		//alert(Obj.titile);
		      	});
		      },
		      'error': function () {},//请求失败后执行的函数
		      'jsonpCallback': 'jsonp' + (new Date()).valueOf().toString().substr(-4),//通过jsonp跨域请求的回调函数名
		      'beforeSend': (function (request) {
				//请求前要处理的函数,使用bind改变this指向
				this.setReqHeader(request);

		      }).bind(this)
  		});

	},
	postData:function(_id,_data,_then,_isAsync){
		//todo
		this.apiConfig['id'] = _id;
		this.apiConfig['type'] = 'post';
		this.apiConfig['async'] = _isAsync||true;
		this.handle(this.apiConfig,_data,_then);
	},
	getData:function(_id,_data,_then,_isAsync){
		//todo
		this.apiConfig['id'] = _id;
		this.apiConfig['type'] = 'get';
		this.apiConfig['async'] = _isAsync||true;
		this.handle(this.apiConfig,_data,_then);
	}
	
}


